package com.fpmsa.officeweb.service;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.fpmsa.officeweb.constant.OWConstant;
import com.fpmsa.officeweb.core.util.UserAgentUtil;
import com.fpmsa.officeweb.model.News;
import com.fpmsa.officeweb.model.NewsCategory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.ui.Model;

import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @Auther: Huang Yuehong
 * @Description:
 * @Date: 2018/6/20 10:32
 */
@Service
public class OfficeWebService {

    @Autowired
    private INewsService newsService;

    @Autowired
    private INewsCategoryService newsCategoryService;

    @Autowired
    private INewsThemeService newsThemeService;

    /**
     * 获取“关于协会”下拉菜单
     */
    public List<NewsCategory> aboutCategories(HttpServletRequest request) {
        Long about_themeId = (Long) newsThemeService.getIdByName(OWConstant.OW_ABOUT).get(0).get("id");
        List<NewsCategory> aboutCategories = newsCategoryService.getCategoriesByThemeId(about_themeId,request);
        return aboutCategories;
    }

    /**
     * 获取“新闻资讯”下拉菜单
     */
    public List<NewsCategory> newsCategories(HttpServletRequest request) {
        Long news_themeId = (Long) newsThemeService.getIdByName(OWConstant.OW_NEWS).get(0).get("id");
        List<NewsCategory> newsCategories = newsCategoryService.getCategoriesByThemeId(news_themeId,request);
        return newsCategories;
    }

    /**
     * 获取“新时代经理”下拉菜单
     */
    public List<NewsCategory> npmesCategories(HttpServletRequest request) {
        Long news_themeId = (Long) newsThemeService.getIdByName(OWConstant.OW_NPMES).get(0).get("id");
        List<NewsCategory> npmesCategories = newsCategoryService.getCategoriesByThemeId(news_themeId,request);
        return npmesCategories;
    }

    /**
     * 获取“专家团队”下拉菜单
     */
    public List<NewsCategory> teamCategories(HttpServletRequest request) {
        Long news_themeId = (Long) newsThemeService.getIdByName(OWConstant.OW_TEAM).get(0).get("id");
        List<NewsCategory> teamCategories = newsCategoryService.getCategoriesByThemeId(news_themeId,request);
        return teamCategories;
    }

    /**
     * 获取“党建一家”下拉菜单
     */
    public List<NewsCategory> partyCategories(HttpServletRequest request) {
        Long news_themeId = (Long) newsThemeService.getIdByName(OWConstant.OW_PARTYBUILD).get(0).get("id");
        List<NewsCategory> partyCategories = newsCategoryService.getCategoriesByThemeId(news_themeId,request);
        return partyCategories;
    }

    /**
     * 获取“会员中心”下拉菜单
     */
    public List<NewsCategory> memberCategories(HttpServletRequest request) {
        Long news_themeId = (Long) newsThemeService.getIdByName(OWConstant.OW_MEMBERCENTER).get(0).get("id");
        List<NewsCategory> memberCategories = newsCategoryService.getCategoriesByThemeId(news_themeId,request);
        return memberCategories;
    }

    /**
     * 往页面的model对象里面添加“关键词”、“描述”、“title”、下拉框选项等属性,用request判断请求的来源（pc/移动端/微信端）
     */
    public Model addAttrForModel(HttpServletRequest request,Model model){

        // 设置页面keyword、description
        model.addAttribute("title_suffix",OWConstant.OW_TITLE_SUFFIX);
        model.addAttribute("keywords",OWConstant.OW_KEYWORDS);
        model.addAttribute("description",OWConstant.OW_DESCRIPTION);

        // 主题菜单下拉框
        model.addAttribute("aboutCategories",aboutCategories(request));
        model.addAttribute("newsCategories",newsCategories(request));
        model.addAttribute("npmesCategories",npmesCategories(request));
        model.addAttribute("teamCategories",teamCategories(request));
        model.addAttribute("partyCategories",partyCategories(request));
        model.addAttribute("memberCategories",memberCategories(request));

        return model;
    }


    /**
     * 往页面的model对象里面添加 flagname、optionName、isNotPc,用request判断请求的来源（pc/移动端/微信端）
     */
    public Model addFOIForModel(HttpServletRequest request,Model model,String flagname,String optionName){

        // 用来判断页面上将左边选中的菜单文字设置成蓝色（class=on）
        model.addAttribute("flagname",flagname);

        // 用来判断页面上将右边蓝色横条选中的选项设置成蓝色（class=on）
        model.addAttribute("optionName",optionName);

        // 判断请求是否来自移动端
        model.addAttribute("isNotPc",UserAgentUtil.isMobile(request));

        return model;
    }

    /**
     * 获取“关于协会”下拉菜单Json
     */
    /*public String aboutCategoryMapsJson() {
        Long about_themeId = (Long) newsThemeService.getIdByName(OWConstant.OW_ABOUT).get(0).get("id");
        List<Map<String,Object>> aboutCategoryMaps = newsCategoryService.getCategoriesByThemeId(about_themeId);
        //String aboutJson = JSON.toJSONString(aboutCategoryMaps).replace("\"","\'");
        String aboutJson = JSON.toJSONString(aboutCategoryMaps);
        return aboutJson;
    }*/

    /**
     * 获取“新闻资讯”下拉菜单Json
     */
    /*public String newsCategoryMapsJson() {
        Long news_themeId = (Long) newsThemeService.getIdByName(OWConstant.OW_NEWS).get(0).get("id");
        List<Map<String,Object>> newsCategoryMaps = newsCategoryService.getCategoriesByThemeId(news_themeId);
        //String newsJson = JSON.toJSONString(newsCategoryMaps).replace("\"","\'");
        String newsJson = JSON.toJSONString(newsCategoryMaps);
        return newsJson;
    }*/

    /**
     * 获取“新时代经理”下拉菜单Json
     */
   /* public String npmesCategoryMapsJson() {
        Long news_themeId = (Long) newsThemeService.getIdByName(OWConstant.OW_NPMES).get(0).get("id");
        List<Map<String,Object>> npmesCategoryMaps = newsCategoryService.getCategoriesByThemeId(news_themeId);
        //String npmesJson = JSON.toJSONString(npmesCategoryMaps).replace("\"","\'");
        String npmesJson = JSON.toJSONString(npmesCategoryMaps);
        return npmesJson;
    }*/

    /**
     * 获取“专家团队”下拉菜单Json
     */
    /*public String teamCategoryMapsJson() {
        Long news_themeId = (Long) newsThemeService.getIdByName(OWConstant.OW_TEAM).get(0).get("id");
        List<Map<String,Object>> teamCategoryMaps = newsCategoryService.getCategoriesByThemeId(news_themeId);
        //String teamJson = JSON.toJSONString(teamCategoryMaps).replace("\"","\'");
        String teamJson = JSON.toJSONString(teamCategoryMaps);
        return teamJson;
    }*/

    /**
     * 获取“党建一家”下拉菜单Json
     */
    /*public String partyCategoryMapsJson() {
        Long news_themeId = (Long) newsThemeService.getIdByName(OWConstant.OW_PARTYBUILD).get(0).get("id");
        List<Map<String,Object>> partyCategoryMaps = newsCategoryService.getCategoriesByThemeId(news_themeId);
        //String partyJson = JSON.toJSONString(partyCategoryMaps).replace("\"","\'");
        String partyJson = JSON.toJSONString(partyCategoryMaps);
        return partyJson;
    }*/

    /**
     * 获取“会员中心”下拉菜单Json
     */
    /*public String memberCategoryMapsJson() {
        Long news_themeId = (Long) newsThemeService.getIdByName(OWConstant.OW_MEMBERCENTER).get(0).get("id");
        List<Map<String,Object>> memberCategoryMaps = newsCategoryService.getCategoriesByThemeId(news_themeId);
        //String memberJson = JSON.toJSONString(memberCategoryMaps).replace("\"","\'");
        String memberJson = JSON.toJSONString(memberCategoryMaps);
        return memberJson;
    }*/


    /**
     * 获取一条某一类别下“置顶”状态的新闻
     */
    public News getTopNewsByC(String categoryName){
        // 用类别名称获取类别id
        EntityWrapper<NewsCategory> wrapper1 = new EntityWrapper<>();
        wrapper1.eq("name",categoryName);
        wrapper1.eq("is_del",0);
        Long category = null;
        if (newsCategoryService.selectOne(wrapper1) != null){
            category = newsCategoryService.selectOne(wrapper1).getId();
        }

        // 获取某一类别下“置顶”状态的一条新闻
        EntityWrapper<News> wrapper = new EntityWrapper<>();
        wrapper.eq("category",category);
        wrapper.eq("is_top",1);
        wrapper.eq("is_del",0);
        News topNews = newsService.selectOne(wrapper);

        return topNews;
    }

    /**
     * 获取category类别下,最新/最旧的num条新闻对象，num为null则获取全部
     */
    public List<News> getNumNewsByCategory(String categoryStr,Integer num,boolean isAsc){
        // 用类别名称获取类别id
        EntityWrapper<NewsCategory> wrapper1 = new EntityWrapper<>();
        wrapper1.eq("name",categoryStr);
        wrapper1.eq("is_Del",0);
        wrapper1.last("limit 1");
        NewsCategory newsCategory = newsCategoryService.selectOne(wrapper1);

        // 获取新闻对象列表
        EntityWrapper<News> wrapper2 = new EntityWrapper<>();
        wrapper2.eq("category",newsCategory.getId());
        wrapper2.eq("is_top",0);
        wrapper2.eq("is_del",0);
        wrapper2.orderBy("create_time",isAsc);

        // num 为空的时候查询全部的新闻
        if (num != null){
            wrapper2.last("limit "+num);
        }

        List<News> newsList = newsService.selectList(wrapper2);
        return newsList;
    }


    /**
     * 获取category类别下num条的新闻(按照时间降序排列)，num为null时查询全部
     */
    public List<News> getNewsList(String categoryStr,Integer num,boolean isAsc){
        // 用类别名称获取类别id
        EntityWrapper<NewsCategory> wrapper1 = new EntityWrapper<>();
        wrapper1.eq("name",categoryStr);
        wrapper1.eq("is_Del",0);
        wrapper1.last("limit 1");
        NewsCategory newsCategory = newsCategoryService.selectOne(wrapper1);

        // 获取新闻对象列表
        EntityWrapper<News> wrapper2 = new EntityWrapper<>();
        wrapper2.eq("category",newsCategory.getId());
        wrapper2.eq("is_del",0);
        wrapper2.orderBy("create_time",isAsc);
        if (num != null){
            wrapper2.last("limit "+ num);
        }

        List<News> newsList = newsService.selectList(wrapper2);
        return newsList;
    }

    /**
     * 转换装有news对象的list中creat_time为某一个格式，然后作为map的list对象返回
     */
    public List<Map<String,Object>> formatCreateTime(List<News> newsList,String formatStr){
        List<Map<String,Object>> newsMapList = new ArrayList<>();
        SimpleDateFormat sdf = new SimpleDateFormat(formatStr);
        for (News news : newsList){
            Map<String,Object> map = new HashMap<>();
            map.put("id",news.getId());
            map.put("title",news.getTitle());

            // 获取类别名称
            String categoryName = newsCategoryService.selectById(news.getCategory()).getName();
            map.put("category",news.getCategory());
            map.put("categoryName",categoryName);
            map.put("picture",news.getPicture());
            map.put("description",news.getDescription());
            map.put("transmitFrom",news.getTransmitFrom());
            map.put("author",news.getAuthor());
            map.put("content",news.getContent());
            map.put("dateTime",sdf.format(news.getCreateTime()));
            map.put("isShow",news.getIsShow());
            map.put("isTop",news.getIsTop());

            newsMapList.add(map);
        }
        return newsMapList;
    }


    /**
     * @description 根据条件“title”模糊搜索相关的新闻
     * @author Huang Yuehong
     * @version V1.0.0
     * @since 2018-06-29 08:07:33
     */
    public List<News> getSearchedNews(String condition,Integer num){
        EntityWrapper<News> wrapper = new EntityWrapper<>();
        wrapper.eq("is_del",0);
        wrapper.like("title",condition);
        wrapper.notIn("category",11);//去掉“资质荣誉”类别
        wrapper.orderBy("create_time",false);
        if (num != null){
            wrapper.last("limit "+ num);
        }

        List<News> newsList = newsService.selectList(wrapper);
        return newsList;
    }


    /**
     * @description 获取num条category类别的新闻纪录，按orderval字段升序排序
     * @author Huang Yuehong
     * @version V1.0.0
     * @since 2018-07-03 03:20:07
     */
    public List<News> getNewsOrderByVal(String categoryStr,Integer num,boolean isAsc){
        // 用类别名称获取类别id
        EntityWrapper<NewsCategory> wrapper1 = new EntityWrapper<>();
        wrapper1.eq("name",categoryStr);
        wrapper1.eq("is_del",0);
        wrapper1.last("limit 1");
        NewsCategory newsCategory = newsCategoryService.selectOne(wrapper1);

        // 获取新闻对象列表
        EntityWrapper<News> wrapper2 = new EntityWrapper<>();
        wrapper2.eq("category",newsCategory.getId());
        wrapper2.eq("is_del",0);
        wrapper2.orderBy("orderval",isAsc);

        if (num != null){
            wrapper2.last("limit "+num);
        }

        List<News> newsList = newsService.selectList(wrapper2);
        return newsList;

    }
}
